


clear all
set more off
set memo 4g
set matsize 800
capture log close


cd ~/Crime


log using Sexratio_CHNS.log, text replace

********************************************************************************************

* Sex ratio measures used for Tables 7 in the paper and Table VI in Appendix B: Census 1990 residential sex ratio

********************************************************************************************

	use census90.dta, clear

	ge prov=int(region1/10000)

	tab prov 

	keep if prov==21 | prov==32 | prov==37 | prov==41 | prov==42 | prov==43 | prov==45 | prov==52

	keep prov birthyr sex hukou

	replace birthyr=birthyr+1000

	replace sex=0 if sex==2 

	ge rural=(hukou==1)

	drop if birthyr<1900

	drop hukou

	compress

	save census_90.dta, replace

	clear

	use census_90.dta, clear

	tab sex

	tab rural

	keep if rural==0

	rename sex male

	rename prov region

	g n=_n

	collapse (count) n, by (region birthyr male)

	reshape wide n, i(region birthyr) j(male)

	rename n0 n_female

	rename n1 n_male

	g age=2000-birthyr

	sort region birthyr

	tsset region birthyr

	g x1=l.n_f
	g x2=l2.n_f

	tsset region age

	g x3=l.n_f
	g x4=l2.n_f

	g x=n_f+x1+x2+x3+x4


	sort region birthyr

	tsset region birthyr

	g y1=n_m
	g y2=l.n_m
	g y3=l2.n_m
	g y4=l3.n_m
	g y5=l4.n_m

	g y=y1+y2+y3+y4+y5

	g ratio_fe_90_urb_5=y/x
	g fe_90_urb_5=x+y
	g ln_fe_90_urb_5=ln(x+y)


	keep region birthyr ratio fe_90_urb_5 ln_fe_90_urb_5

	save ratio_fe_90_urb_5, replace

	clear

	use census_90.dta, clear

	tab sex

	tab rural

	keep if rural==1

	rename sex male

	rename prov region

	g n=_n

	collapse (count) n, by (region birthyr male)

	reshape wide n, i(region birthyr) j(male)

	rename n0 n_female

	rename n1 n_male

	g age=2000-birthyr

	sort region birthyr

	tsset region birthyr

	g x1=l.n_f
	g x2=l2.n_f

	tsset region age

	g x3=l.n_f
	g x4=l2.n_f

	g x=n_f+x1+x2+x3+x4


	sort region birthyr

	tsset region birthyr

	g y1=n_m
	g y2=l.n_m
	g y3=l2.n_m
	g y4=l3.n_m
	g y5=l4.n_m

	g y=y1+y2+y3+y4+y5

	g ratio_fe_90_rur_5=y/x
	g fe_90_rur_5=x+y
	g ln_fe_90_rur_5=ln(x+y)


	keep region birthyr ratio fe_90_rur_5 ln_fe_90_rur_5

	save ratio_fe_90_rur_5, replace

	clear
	
	use census_90.dta, clear

	tab sex

	tab rural

	keep if rural==0

	rename sex male

	rename prov region

	g n=_n

	collapse (count) n, by (region birthyr male)

	reshape wide n, i(region birthyr) j(male)

	rename n0 n_female

	rename n1 n_male

	g age=2000-birthyr


	sort region birthyr

	tsset region birthyr

	g x1=l.n_m
	g x2=l2.n_m

	tsset region age

	g x3=l.n_m
	g x4=l2.n_m

	g x=n_m+x1+x2+x3+x4


	sort region age

	tsset region age

	g y1=n_f
	g y2=l.n_f
	g y3=l2.n_f
	g y4=l3.n_f
	g y5=l4.n_f

	g y=y1+y2+y3+y4+y5

	g ratio_ma_90_urb_5=x/y
	g ma_90_urb_5=x+y
	g ln_ma_90_urb_5=ln(x+y)

	keep region birthyr ratio ma_90_urb_5 ln_ma_90_urb_5

	save ratio_ma_90_urb_5, replace

	clear
	
	use census_90.dta, clear

	tab sex

	tab rural

	keep if rural==1

	rename sex male

	rename prov region

	g n=_n

	collapse (count) n, by (region birthyr male)

	reshape wide n, i(region birthyr) j(male)

	rename n0 n_female

	rename n1 n_male

	g age=2000-birthyr


	sort region birthyr

	tsset region birthyr

	g x1=l.n_m
	g x2=l2.n_m

	tsset region age

	g x3=l.n_m
	g x4=l2.n_m

	g x=n_m+x1+x2+x3+x4


	sort region age

	tsset region age

	g y1=n_f
	g y2=l.n_f
	g y3=l2.n_f
	g y4=l3.n_f
	g y5=l4.n_f

	g y=y1+y2+y3+y4+y5

	g ratio_ma_90_rur_5=x/y
	g ma_90_rur_5=x+y
	g ln_ma_90_rur_5=ln(x+y)

	keep region birthyr ratio ma_90_rur_5 ln_ma_90_rur_5

	save ratio_ma_90_rur_5, replace

	clear
	
	

	use ratio_fe_90_rur_5.dta, clear
	
	keep birthyr region ratio
	rename ratio ratio
	g rural=1
	g male=0
	save temp1.dta, replace
	clear
	
	
	use ratio_fe_90_urb_5.dta, clear
	
	keep birthyr region ratio
	rename ratio ratio
	g rural=0
	g male=0
	save temp2.dta, replace
	clear
	
	
	use ratio_ma_90_rur_5.dta, clear
	
	keep birthyr region ratio
	rename ratio ratio
	g rural=1
	g male=1
	save temp3.dta, replace
	clear
	
	
	use ratio_ma_90_urb_5.dta, clear
	
	keep birthyr region ratio
	rename ratio ratio
	g rural=0
	g male=1
	save temp4.dta, replace
	clear
	
	use temp1.dta, clear
	append using temp2 temp3 temp4
	
	rename birthyr yob
	rename region id
	
	keep if yob>=1944 & yob<=1971
	
	save Sexratio_CHNS_1990.dta, replace
	
	clear
	
	
********************************************************************************************

* Sex ratio measures used for Table VII in Appendix B: Census 2000 birth sex ratio

********************************************************************************************     
        
        use census00.dta, clear
        keep id r03 r041 r07 r08* 
        drop if id==.
        ge rural=r07==1 
        drop r07 
        
        rename r041 byear
        keep if byear>=1940 & byear<=1979
        
        ge prov=int(id/(10^16))      
                                                                                                                                                                                               
        * generate birth province
        
        ge bprov=prov
        replace bprov=r082 if r081==3 
        drop prov 
        
        keep if bprov==21|bprov==32|bprov==37|bprov==41|bprov==42|bprov==43|bprov==45|bprov==52
        
        
        ge male=r03==1
        drop r03 
        
        
        save SRxCHNSbprov, replace 
        
        clear
        
        use SRxCHNSbprov, clear 
             
        
        forvalues y=1942(1)1975 { 
            use  if  byear>=`y'-2 & byear<=`y'+2 & male==1 using SRxCHNSbprov, clear 
            collapse (sum) men=male, by(bprov rural)
            ge byear=`y'
            
            sort byear rural bprov 
            save yob`y'm, replace
        }
        
        
        forvalues y=1942(1)1975 { 
            use  if  byear>=`y' & byear<=`y'+4 & male==0 using SRxCHNSbprov, clear 
            collapse (count) wom=male, by(bprov rural)
            ge byear=`y'
            
            sort byear rural bprov 
            save yob`y'w, replace
        }
        
        use yob1942m
        forvalues y=1943(1)1975 { 
            append using yob`y'm
        }
        
        sort byear rural bprov 
        save temp, replace 
        
        use yob1942w
        forvalues y=1943(1)1975 { 
            append using yob`y'w
        }
        sort byear rural bprov 
        merge byear rural bprov using temp 
        tab _merge 
        drop _merge 
        
        ge ratiob=men/wom 
        ge male=1 
        sort byear rural bprov male 
        save CHNSbprovm, replace 
        
        use CHNSbprovm, clear 
        replace male=0 
        replace byear=byear+2 /* a woman's sex ratio is that of a two year older man */
        sort byear rural bprov male 
        merge byear rural bprov male using CHNSbprovm 
        tab _merge 
        drop _merge 
        sort byear rural bprov male 
		
		keep bprov byear rural male ratiob
		
		rename bprov id
		rename byear yob
		
        save Sexratio_CHNS_2000.dta, replace
		
		
	clear


log close
